package com.example.controller.customer;

import com.example.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * 1. @ClassDescription:
 * 2. @author: Z
 * 3. @date: 2024年05月14日 18:39
 */
@Api(tags = "通用接口")
@Slf4j
@RequestMapping("/customer/common")
@RestController("customerCommon")
public class CommonController {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/redisClean")
    @ApiOperation("清空缓存")
    public Result cleanCache(){
        boolean state = false;
        Set<String> keys = redisTemplate.keys("*");
        for (String key : keys) {
            if(key.indexOf("order")!= -1)
                continue;
            redisTemplate.delete(key);
            state = true;
        }
        if (state) {
            return Result.success(null,"清除缓存成功！");
        } else {
            return Result.error("无缓存数据可清除！");
        }
    }
}
